/*
 * Decompiled with CFR 0.150.
 * 
 * Could not load the following classes:
 *  cn.hutool.core.collection.CollUtil
 *  cn.hutool.core.util.ObjectUtil
 *  cn.hutool.core.util.StrUtil
 *  com.alibaba.fastjson.JSON
 *  de.felixroske.jfxsupport.FXMLController
 *  javafx.beans.value.ChangeListener
 *  javafx.beans.value.ObservableValue
 *  javafx.collections.FXCollections
 *  javafx.collections.ObservableList
 *  javafx.fxml.FXML
 *  javafx.fxml.Initializable
 *  javafx.scene.Node
 *  javafx.scene.control.Alert
 *  javafx.scene.control.Alert$AlertType
 *  javafx.scene.control.CheckBox
 *  javafx.scene.control.ChoiceBox
 *  javafx.scene.control.RadioButton
 *  javafx.scene.control.TableCell
 *  javafx.scene.control.TableColumn
 *  javafx.scene.control.TableView
 *  javafx.scene.control.TextField
 *  javafx.scene.control.cell.PropertyValueFactory
 *  javafx.scene.layout.AnchorPane
 *  javafx.util.Callback
 *  javafx.util.StringConverter
 *  org.springframework.beans.factory.annotation.Autowired
 *  org.springframework.util.CollectionUtils
 */
package com.gys.controller.physicalCount;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.gys.common.business.ProductBasicUtil;
import com.gys.common.business.ProductLocationUtil;
import com.gys.common.data.HttpJson;
import com.gys.controller.BaseController;
import com.gys.controller.physicalCount.AddPhysicalController;
import com.gys.data.physicalCount.GetPhysicalCountOutData;
import com.gys.data.productBasic.GetProductInfoQueryInData;
import com.gys.data.productBasic.GetProductInfoQueryOutData;
import com.gys.data.productLocation.GetProductLocationInData;
import com.gys.data.productLocation.GetProductLocationOutData;
import de.felixroske.jfxsupport.FXMLController;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.ResourceBundle;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.control.Alert;
import javafx.scene.control.CheckBox;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.RadioButton;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.AnchorPane;
import javafx.util.Callback;
import javafx.util.StringConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;

@FXMLController
public class ChoosePhysicalProductController
extends BaseController
implements Initializable {
    @FXML
    private AnchorPane anchorPane;
    @FXML
    private TextField searchProId;
    @FXML
    private ChoiceBox<GetProductLocationOutData> searchArea;
    @FXML
    private ChoiceBox<GetProductLocationOutData> searchGroup;
    @FXML
    private ChoiceBox<GetProductLocationOutData> searchShelf;
    @FXML
    private ChoiceBox<GetProductLocationOutData> searchStorey;
    @FXML
    private ChoiceBox<GetProductLocationOutData> searchSeat;
    @FXML
    private TableView<GetProductInfoQueryOutData> tableView;
    @FXML
    private TableColumn<GetProductInfoQueryOutData, CheckBox> checkBox;
    @FXML
    private TableColumn<GetProductInfoQueryOutData, String> proCode;
    @FXML
    private TableColumn<GetProductInfoQueryOutData, String> proName;
    @FXML
    private TableColumn<GetProductInfoQueryOutData, String> gsplArea;
    @FXML
    private TableColumn<GetProductInfoQueryOutData, String> gsplGroup;
    @FXML
    private TableColumn<GetProductInfoQueryOutData, String> gsplShelf;
    @FXML
    private TableColumn<GetProductInfoQueryOutData, String> gsplStorey;
    @FXML
    private TableColumn<GetProductInfoQueryOutData, String> gsplSeat;
    @FXML
    private TableColumn<GetProductInfoQueryOutData, String> batchNo;
    @FXML
    private TableColumn<GetProductInfoQueryOutData, String> stockQty;
    @FXML
    private RadioButton radioProCode;
    @FXML
    private RadioButton radioProLocation;
    private ObservableList<GetProductInfoQueryOutData> observableTableList = FXCollections.observableArrayList(new ArrayList());
    @Autowired
    private AddPhysicalController addPhysicalController;

    public void initialize(URL location, ResourceBundle resources) {
        this.init();
    }

    @Override
    public void init() {
        if (ObjectUtil.isNull(this.anchorPane)) {
            return;
        }
        this.searchProId.setText("");
        Alert alert = this.createAlert(Alert.AlertType.INFORMATION, "\u63d0\u793a\u4fe1\u606f", null);
        GetProductLocationInData inData = new GetProductLocationInData();
        HttpJson rsp = ProductLocationUtil.selectLocationDrop(inData);
        if (rsp.isSuccess()) {
            GetProductLocationOutData outData = (GetProductLocationOutData)JSON.parseObject((String)rsp.getMsg(), GetProductLocationOutData.class);
            List<GetProductLocationOutData> areaList = outData.getAreaList();
            this.searchArea.setItems(FXCollections.observableArrayList(areaList));
            this.searchArea.converterProperty().set(new StringConverter<GetProductLocationOutData>(){

                public String toString(GetProductLocationOutData object) {
                    return object.getName();
                }

                public GetProductLocationOutData fromString(String string) {
                    return null;
                }
            });
            List<GetProductLocationOutData> groupList = outData.getGroupList();
            this.searchGroup.setItems(FXCollections.observableArrayList(groupList));
            this.searchGroup.converterProperty().set(new StringConverter<GetProductLocationOutData>(){

                public String toString(GetProductLocationOutData object) {
                    return object.getName();
                }

                public GetProductLocationOutData fromString(String string) {
                    return null;
                }
            });
            List<GetProductLocationOutData> shelfList = outData.getShelfList();
            this.searchShelf.setItems(FXCollections.observableArrayList(shelfList));
            this.searchShelf.converterProperty().set(new StringConverter<GetProductLocationOutData>(){

                public String toString(GetProductLocationOutData object) {
                    return object.getName();
                }

                public GetProductLocationOutData fromString(String string) {
                    return null;
                }
            });
            List<GetProductLocationOutData> storeyList = outData.getStoreyList();
            this.searchStorey.setItems(FXCollections.observableArrayList(storeyList));
            this.searchStorey.converterProperty().set(new StringConverter<GetProductLocationOutData>(){

                public String toString(GetProductLocationOutData object) {
                    return object.getName();
                }

                public GetProductLocationOutData fromString(String string) {
                    return null;
                }
            });
            List<GetProductLocationOutData> seatList = outData.getSeatList();
            this.searchSeat.setItems(FXCollections.observableArrayList(seatList));
            this.searchSeat.converterProperty().set(new StringConverter<GetProductLocationOutData>(){

                public String toString(GetProductLocationOutData object) {
                    return object.getName();
                }

                public GetProductLocationOutData fromString(String string) {
                    return null;
                }
            });
        } else {
            alert.setContentText(rsp.getMsg());
            alert.showAndWait();
        }
        this.tableView.getItems().clear();
    }

    @FXML
    private void search() {
        HttpJson rsp;
        Alert alert = this.createAlert(Alert.AlertType.INFORMATION, "\u63d0\u793a\u4fe1\u606f", null);
        if (!this.radioProCode.isSelected() && !this.radioProLocation.isSelected()) {
            alert.setContentText("\u8bf7\u9009\u62e9\u5546\u54c1\u7f16\u7801\u6216\u8005\u5546\u54c1\u8d27\u4f4d\u67e5\u8be2");
            alert.showAndWait();
            return;
        }
        GetProductInfoQueryInData inData = new GetProductInfoQueryInData();
        if (this.radioProCode.isSelected()) {
            if (StrUtil.isBlank((CharSequence)this.searchProId.getText().trim())) {
                alert.setContentText("\u8bf7\u8f93\u5165\u5546\u54c1\u7f16\u7801");
                alert.showAndWait();
                return;
            }
            inData.setGspgProId(this.searchProId.getText().trim());
        }
        if (this.radioProLocation.isSelected()) {
            if (ObjectUtil.isNull(this.searchArea.getValue()) && ObjectUtil.isNull(this.searchGroup.getValue()) && ObjectUtil.isNull(this.searchShelf.getValue()) && ObjectUtil.isNull(this.searchStorey.getValue()) && ObjectUtil.isNull(this.searchSeat.getValue())) {
                alert.setContentText("\u8bf7\u9009\u62e9\u5546\u54c1\u8d27\u4f4d");
                alert.showAndWait();
                return;
            }
            if (ObjectUtil.isNotNull(this.searchArea.getValue())) {
                inData.setGsplArea(((GetProductLocationOutData)this.searchArea.getValue()).getName());
            }
            if (ObjectUtil.isNotNull(this.searchGroup.getValue())) {
                inData.setGsplGroup(((GetProductLocationOutData)this.searchGroup.getValue()).getName());
            }
            if (ObjectUtil.isNotNull(this.searchShelf.getValue())) {
                inData.setGsplShelf(((GetProductLocationOutData)this.searchShelf.getValue()).getName());
            }
            if (ObjectUtil.isNotNull(this.searchStorey.getValue())) {
                inData.setGsplStorey(((GetProductLocationOutData)this.searchStorey.getValue()).getName());
            }
            if (ObjectUtil.isNotNull(this.searchSeat.getValue())) {
                inData.setGsplSeat(((GetProductLocationOutData)this.searchSeat.getValue()).getName());
            }
        }
        if ((rsp = ProductBasicUtil.selectPhysicalProList(inData)).isSuccess()) {
            List<GetProductInfoQueryOutData> dataList = JSON.parseArray((String)rsp.getMsg(), GetProductInfoQueryOutData.class);
            int index = 1;
            for (GetProductInfoQueryOutData data : dataList) {
                data.setIndex(index);
                ++index;
            }
            this.observableTableList.clear();
            this.observableTableList = FXCollections.observableArrayList((Collection)dataList);
            this.bingTableCell();
            this.tableView.setItems(this.observableTableList);
        } else {
            alert.setContentText(rsp.getMsg());
            alert.showAndWait();
        }
    }

    private void bingTableCell() {
        this.checkBox.setCellValueFactory(cellData -> ((GetProductInfoQueryOutData)cellData.getValue()).getCheckBox().getCustomCheckBox());
        this.checkBox.setCellFactory(col -> {
            TableCell<GetProductInfoQueryOutData, CheckBox> cell = new TableCell<GetProductInfoQueryOutData, CheckBox>(){

                protected void updateItem(CheckBox item, boolean empty) {
                    super.updateItem(item, empty);
                    this.setGraphic((Node)item);
                    if (item != null) {
                        item.setOnMouseClicked(col -> {
                            if (CollUtil.isEmpty((Collection)ChoosePhysicalProductController.this.observableTableList)) {
                                return;
                            }
                            int index = this.getIndex();
                            for (int i = 0; i < ChoosePhysicalProductController.this.observableTableList.size(); ++i) {
                                if (i == index) continue;
                                ((GetProductInfoQueryOutData)ChoosePhysicalProductController.this.observableTableList.get(i)).getCheckBox().setSelectStatus(false);
                            }
                        });
                    }
                }
            };
            return cell;
        });
        this.proCode.setCellValueFactory((Callback)new PropertyValueFactory(this.proCode.getId()));
        this.proName.setCellValueFactory((Callback)new PropertyValueFactory(this.proName.getId()));
        this.gsplArea.setCellValueFactory((Callback)new PropertyValueFactory(this.gsplArea.getId()));
        this.gsplGroup.setCellValueFactory((Callback)new PropertyValueFactory(this.gsplGroup.getId()));
        this.gsplShelf.setCellValueFactory((Callback)new PropertyValueFactory(this.gsplShelf.getId()));
        this.gsplStorey.setCellValueFactory((Callback)new PropertyValueFactory(this.gsplStorey.getId()));
        this.gsplSeat.setCellValueFactory((Callback)new PropertyValueFactory(this.gsplSeat.getId()));
        this.batchNo.setCellValueFactory((Callback)new PropertyValueFactory(this.batchNo.getId()));
        this.stockQty.setCellValueFactory((Callback)new PropertyValueFactory(this.stockQty.getId()));
        this.tableView.getSelectionModel().selectedItemProperty().addListener((ChangeListener)new ChangeListener<GetProductInfoQueryOutData>(){

            public void changed(ObservableValue<? extends GetProductInfoQueryOutData> observable, GetProductInfoQueryOutData oldValue, GetProductInfoQueryOutData newValue) {
                if (CollUtil.isEmpty((Collection)ChoosePhysicalProductController.this.observableTableList)) {
                    return;
                }
                for (int i = 0; i < ChoosePhysicalProductController.this.observableTableList.size(); ++i) {
                    if (i == newValue.getIndex() - 1) {
                        ((GetProductInfoQueryOutData)ChoosePhysicalProductController.this.observableTableList.get(i)).getCheckBox().setSelectStatus(true);
                        continue;
                    }
                    ((GetProductInfoQueryOutData)ChoosePhysicalProductController.this.observableTableList.get(i)).getCheckBox().setSelectStatus(false);
                }
            }
        });
    }

    @FXML
    private void add() {
        Alert alert = this.createAlert(Alert.AlertType.INFORMATION, "\u63d0\u793a\u4fe1\u606f", null);
        List<Integer> indexList = this.getSelectIndex();
        if (CollUtil.isEmpty(indexList)) {
            alert.setContentText("\u8bf7\u9009\u62e9\u8bb0\u5f55");
            alert.showAndWait();
            return;
        }
        GetProductInfoQueryOutData proData = (GetProductInfoQueryOutData)this.observableTableList.get(0);
        if (ObjectUtil.isNull(proData)) {
            alert.setContentText("\u8bf7\u9009\u62e9\u8bb0\u5f55");
            alert.showAndWait();
            return;
        }
        List<GetPhysicalCountOutData> list = this.addPhysicalController.getAllDataList();
        for (GetPhysicalCountOutData data : list) {
            if (!data.getGspcProId().equals(proData.getProCode()) || !data.getBatchNo().equals(proData.getBatchNo())) continue;
            alert.setContentText(StrUtil.format((CharSequence)"\u6240\u9009\u5546\u54c1({})\u6279\u53f7({})\u5df2\u5b58\u5728\uff0c\u4e0d\u53ef\u91cd\u65b0\u6dfb\u52a0", (Object[])new Object[]{data.getGspcProId(), data.getBatchNo()}));
            alert.showAndWait();
            return;
        }
        GetPhysicalCountOutData outData = new GetPhysicalCountOutData();
        outData.setGspcProId(proData.getProCode());
        outData.setProName(proData.getProName());
        outData.setGsplArea(proData.getGsplArea());
        outData.setGsplGroup(proData.getGsplGroup());
        outData.setGsplShelf(proData.getGsplShelf());
        outData.setGsplStorey(proData.getGsplStorey());
        outData.setGsplSeat(proData.getGsplSeat());
        outData.setBatchNo(proData.getBatchNo());
        outData.setGsppPriceNormal(proData.getGsppPriceNormal());
        outData.setVaild(proData.getVaild());
        outData.setStockQty(proData.getStockQty());
        outData.setProSpecs(proData.getProSpecs());
        outData.setProUnit(proData.getProUnit());
        outData.setProFactoryName(proData.getProFactoryName());
        outData.setProPlace(proData.getProPlace());
        outData.setProForm(proData.getProForm());
        outData.setProRegisterNo(proData.getProRegisterNo());
        this.addPhysicalController.addPhysicalProduct(outData);
        this.closeWindow();
    }

    @FXML
    private void closeWindow() {
        this.anchorPane.getScene().getWindow().hide();
    }

    private List<Integer> getSelectIndex() {
        ArrayList<Integer> ids = new ArrayList<Integer>();
        if (!CollectionUtils.isEmpty(this.observableTableList)) {
            for (GetProductInfoQueryOutData i : this.observableTableList) {
                if (!i.getCheckBox().isSelected().booleanValue()) continue;
                ids.add(i.getIndex());
            }
        }
        return ids;
    }

    @FXML
    private void radioProCodeAction() {
        this.radioProLocation.setSelected(false);
        this.searchArea.setValue(null);
        this.searchGroup.setValue(null);
        this.searchShelf.setValue(null);
        this.searchStorey.setValue(null);
        this.searchSeat.setValue(null);
    }

    @FXML
    private void radioProLocationAction() {
        this.radioProCode.setSelected(false);
        this.searchProId.clear();
    }
}

